const WxPay = require('wechatpay-node-v3'); // 支持使用require
const fs = require('fs')
const path = require('path')

const publicKeyPath = path.join(__dirname, './lib/cert/apiclient_cert.pem')
const privateKeyPath = path.join(__dirname, './lib/cert/apiclient_key.pem')

const payParams = async ({
	openid
}) => {
	//配置支付的sdk
	const pay = new WxPay({
		appid: 'wx52d48be4d68f58a2',
		mchid: '1628245715',
		publicKey: fs.readFileSync(publicKeyPath), // 公钥
		privateKey: fs.readFileSync(privateKeyPath), // 秘钥
	});
	try {

		const params = {
			description: '测试商品',
			out_trade_no: "wx20180101abcdefg",
			notify_url: 'http://localhost:5173',
			amount: {
				total: 1,
			},
			payer: {
				openid,
			},
			scene_info: {
				payer_client_ip: 'ip',
			},
		};

		let res = await pay.transactions_jsapi(params)

		return res;

	} catch (e) {
		console.log(e);
		//TODO handle the exception
	}
	// await 
}

exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event)

	let params = await payParams({
		...event
	})
	//返回数据给客户端
	return params
};